﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace Kargo3504
{
    public partial class FrmDBAyar : Form
    {
        public FrmDBAyar()
        {
            InitializeComponent();

        }
        string DBName = "";
        private void btnBaglan_Click(object sender, EventArgs e)
        {
            cmdDatabaseNames.Items.Clear();


            string conString = string.Empty;


            conString = string.Format("Data Source={0};UID={1};PWD={2}", txtServerIP.Text, txtUserID.Text, txtPassword.Text);

            using (SqlConnection sqlConx = new SqlConnection(conString))
            {
                sqlConx.Open();
                DataTable tblDatabases = sqlConx.GetSchema("Databases");
                sqlConx.Close();
                for (int i = 0; i < tblDatabases.Rows.Count; i++)
                {
                    cmdDatabaseNames.Items.Add(tblDatabases.Rows[i]["database_name"].ToString());
                }
            }

            cmdDatabaseNames.SelectedIndex = cmdDatabaseNames.Items.IndexOf(DBName);

            panel1.Enabled = false;
            panel2.Enabled = true;
            cbDuzenle.Enabled = false;
        }

        private void FrmDBAyar_Load(object sender, EventArgs e)
        {
            panel1.Enabled = false;
            panel2.Enabled = false;

            string cnnstrng = ConfigurationManager.ConnectionStrings["Kargo3504DBConnectionString"].ConnectionString;
            //Data Source=.;Initial Catalog=Kargo3504DB;User ID=sa; Password=1234567?

            string[] Parcali = cnnstrng.Split(';');
            txtServerIP.Text = Parcali[0].Substring(12);
            DBName = Parcali[1].Substring(16);
            cmdDatabaseNames.Items.Add(DBName);
            cmdDatabaseNames.SelectedIndex = 0;
            txtUserID.Text = Parcali[2].Substring(8);
            txtPassword.Text = Parcali[3].Substring(10);

        }

        private void btnAyarla_Click(object sender, EventArgs e)
        {
            string cnn = "Data Source=" + txtServerIP.Text + ";Initial Catalog=" + cmdDatabaseNames.SelectedItem.ToString() + ";User ID=" + txtUserID.Text + "; Password=" + txtPassword.Text; ;

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.ConnectionStrings.ConnectionStrings.Remove("Kargo3504DBConnectionString");
            config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings() { ConnectionString = cnn, Name = "Kargo3504DBConnectionString" });

            config.Save(ConfigurationSaveMode.Full);


            MessageBox.Show("Işleminiz Başarı İle Değiştirildi.");
            this.Close();
        }

        private void cbDuzenle_CheckedChanged(object sender, EventArgs e)
        {
            if (cbDuzenle.Checked)
            {
                panel1.Enabled = true;
            }
            else
            {
                panel1.Enabled = false;
            }
        }
    }
}
